//
//  TORongUtils.m
//  IMChat
//
//  Created by CLSgGhost on 2021/6/21.
//  Copyright © 2021年 TeamOne. All rights reserved.
//

#import "TORongUtils.h"

@implementation TORongUtils

+ (void)connectRongIM:(NSString *)token
            withBlock:(ConnectBlock)block{
    
    [[RCIM sharedRCIM]
     
     connectWithToken:token

     success:^(NSString *userId) {
         NSLog(@"登录成功。当前登录的用户ID：%@", userId);
         
         // 获取主线程队列
         dispatch_async(dispatch_get_main_queue(),^{
             /** 给全局变量赋值. */
             AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
             myDelegate.userId = userId;
             NSLog(@"Connecting!!!!!!!%@",myDelegate.userId);
         });
         
         // 查询用户好友ID列表，查询完再允许跳转主界面
         [TOAFHttpUtils findUserFriends:userId
                              withBlock:^(NSString * _Nonnull result) {
                                  NSLog(@"%@",result);
                                  if([result isEqualToString:@"success"]){
                                      block(@"success");
                                  }
                              }];
         
         // 查询用户好友请求信息表
         [TOAFHttpUtils findNewFiendRequestInfo:userId];
         //设置当前用户信息，用于显示自己的头像和昵称
         [[TORongUtils alloc] setCurrentUserInfo:userId];
         
     } error:^(RCConnectErrorCode status) {
         NSLog(@"登陆的错误码为:%ld", (long)status);
         block(@"error");
     } tokenIncorrect:^{
         NSLog(@"token错误");
         block(@"error");
     }];
}

- (void)setCurrentUserInfo:(NSString *)userId{
    //设置当前用户信息，用于显示自己的头像和昵称
    [TOAFHttpUtils findUserById:userId
                      withBlock:^(UserInfoModel * _Nonnull userModel) {
                          
                          RCUserInfo *userInfo = [[RCUserInfo alloc]
                                                  initWithUserId:userModel.userId
                                                  name:userModel.userName
                                                  portrait:userModel.userPortraitUri];
                          
                          dispatch_async(dispatch_get_main_queue(),^{
                              /** 获得全局变量. */
                              AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
                              
                              myDelegate.user = userModel;
                              NSLog(@"初始化获得用户对象名%@",myDelegate.user.userName);
                          });
                          
                          [RCIM sharedRCIM].currentUserInfo = userInfo;
                      }];
}


@end
